package Day2025_03_01;

public class LsstCode25 {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode newHead = new ListNode(0);
        ListNode prev = newHead;
        ListNode cur = head;
        int n = 0;
        while (cur != null) {
            cur = cur.next;
            n++;
        }
        cur = head;
        n /= k;
        while (n != 0) {
            ListNode tmp = cur;
            for (int i = 0; i < k; i++) {
                ListNode next = cur.next;
                cur.next = prev.next;
                prev.next = cur;
                cur = next;
            }
            prev = tmp;
            n--;
        }
        prev.next = cur;
        return newHead.next;
    }
}
